class ChargeApplyController{
    constructor($state,$rootScope,$scope,AccountService){
        Object.assign(this,{$state,$rootScope,$scope,AccountService});
        this.$rootScope.currentGroup= this.$state.params.cg;
        this.statusList = [ {value:-9,name:"全部"}, {value:0,name:"已撤单"},
            {value:1,name:"待付款"}, {value:2,name:"处理中"}, {value:3,name:"出票中"},
            {value:4,name:"下单成功"}, {value:5,name:"下单失败" }, {value:6,name:"部份成功" },];
        this["chargeResult"]=null;
        this.charge = {
            amount:0
        };
        this.init();
    }


    /**
     * 查询
     */
    init(){
        let self  = this;
        self.AccountService.getCurrChargeOrders().then(_success).catch(_error);
        function _success(data) {
            self.chargeResult = data.pay_order;
        }
        function _error(reason) {
            console.log(JSON.stringify(reason));
        };
    }

    chargeAgain(){
        let self = this;
        self["chargeResult"]=null;
    }

    cancelCharge(payOrderId){
        let self = this;
        let param = {};
        param.pay_order_id = payOrderId;
        layer.confirm("您确认要取消充值吗？",{
            btn:["确定","不取消了"],
            btn1:function (index,layerro) {
                //执行修改
                layer.close(index);
                self.AccountService.chargeCancel(param).then(_success).catch(_error);
            },
            btn2:function (index,layerro) {
                layer.close(index);
            }
        });
        function _success(data) {
            if(data.success==true){
                layer.open({
                   icon:0,
                   title:"提示",
                   content:data.message.content,
                   btn:["好的"],
                   yes:function (index,layerno) {
                       layer.close(index);
                       self.$state.reload();
                   }
               });
            }
        }
        function _error(reason) {
            layer.alert(reason&&reason.text?reason.text:"对不起操作失败！",{
                title:"错误",
                icon: 2,
                btn:["知道了"],
                btn1:function (index,layerro) {
                    layer.close(index);
                }
            });
        }
    }

    /**
     * 立即充值
     */
    chargeNow(){
        let self = this;
        layer.confirm("请确认要立即充值吗？",{
            btn:["确定","不充值了"],
            btn1:function (index,layerro) {
                //执行修改
                layer.close(index);
                console.log(self.charge);
                self.AccountService.chargeApply(self.charge).then(_success).catch(_error);
            },
            btn2:function (index,layerro) {
                layer.close(index);
            }
        });
        function _success(data) {
            if(data.success==true){
                self["chargeResult"] = data.pay_order;
                console.log(self.chargeResult);
            }
        }
        function _error(reason) {
            layer.alert(reason&&reason.text?reason.text:"对不起操作失败！",{
                title:"错误",
                icon: 2,
                btn:["知道了"],
                btn1:function (index,layerro) {
                    layer.close(index);
                }
            });
        }
    }
}
ChargeApplyController.$inject = ["$state","$rootScope","$scope","AccountService"];

export default ChargeApplyController;